package com.whisper.application.app.web.controller.front;

import com.whisper.application.app.web.entities.LoginBody;
import com.whisper.application.app.web.service.IFrontLoginService;
import com.whisper.application.service.domain.VrUser;
import com.whisper.common.constant.Constants;
import com.whisper.common.core.domain.AjaxResult;
import com.whisper.common.core.domain.entity.SysUser;
import com.whisper.common.security.util.SecurityUtils;
import com.whisper.common.utils.StreamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
public class FrontLoginController {
    @Autowired
    private IFrontLoginService loginService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/signin")
    public void login(@RequestBody LoginBody loginBody, HttpServletResponse response)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        StreamUtil.response(response, ajax);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    public void getInfo(HttpServletResponse response)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        StreamUtil.response(response, ajax);
    }

    @PostMapping("/signup")
    public void register(@RequestBody VrUser user)
    {
        loginService.register(user);
    }
}
